package com.lzx.iteam.net;

import android.content.Context;
import android.os.Message;
import android.util.Log;
import com.lzx.iteam.bean.TalkListData;
import com.lzx.iteam.util.Constants;
import com.lzx.iteam.util.StringUtil;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by zhangshuai on 15/10/21.
 */
public class GetCalendarStatusMsg extends CcMsgStructure {

    private Message mmCallback;
    private Context mContext;

    public GetCalendarStatusMsg(Message callback, Context context) {
        mmCallback = callback;
        mContext = context;
    }

    @Override
    public void onReceive(JSONObject data) {
        int newMsg = 0;
        try {
           // JSONObject item = data.getJSONObject(AsynHttpClient.KEY_CC_DATA);
            if(data.has("calendar_status")){
                newMsg = data.getInt("calendar_status");
                if (mmCallback != null){
                    mmCallback.arg1 = 0;
                }
            }else if (data.has("data")){
                JSONObject item = data.getJSONObject(AsynHttpClient.KEY_CC_DATA);
                if (item.has("count")){
                    newMsg = item.getInt("count");
                    if (mmCallback != null){
                        mmCallback.arg1 = 1;
                    }
                }else if (item.has("unread")){
                    newMsg = item.getInt("unread");
                    if (mmCallback != null){
                        mmCallback.arg1 = 2;
                    }
                }
            }
            if (mmCallback != null) {
                mmCallback.obj = newMsg;
                mmCallback.sendToTarget();
                return;
            }
        } catch (JSONException e) {
            Log.d("calendar_status", e.toString());
            e.printStackTrace();
        }

    }

    @Override
    public void onError(int error, Object ret) {
        String errorMsg = null;
        if (ret != null) {
            JSONObject jData = null;
            try {
                jData = new JSONObject(ret.toString());
                errorMsg = jData.getString("msg");
            } catch (JSONException e) {
                errorMsg = "服务端数据解析失败!";
                e.printStackTrace();
            }

        }
        if (mmCallback != null) {
            mmCallback.arg1 = error;
            if (!StringUtil.isEmpty(errorMsg)) {
                mmCallback.obj = errorMsg;
            } else {
                mmCallback.obj = Constants.NETWORK_WRROR;
            }
            mmCallback.sendToTarget();
        }
    }
}
